<?
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $txt, $scripturl, $settings, $user_info;
global $dbtables;
global $bounty_maxvalue;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

$title = $txt['bnt_by_title'];

// Store GET and POST variables.
if (isset($_GET['response']))
	$response = $_GET['response'];
elseif (isset($_POST['response']))
	$response = $_POST['response'];
else
	$response = 'default';

if (isset($_GET['bounty_on']))
	$bounty_on = $_GET['bounty_on'];
elseif (isset($_POST['bounty_on']))
	$bounty_on = $_POST['bounty_on'];
	
if (isset($_GET['bid']))
	$bid = $_GET['bid'];
	
if (isset($_POST['amount']))
	$amount = $_POST['amount'];

// Pull player info.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

if ($response == "display")
{
	bigtitle($title);
	
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_ships}, {raw:db_bounty}
		WHERE bounty_on = ship_id
		AND bounty_on = {int:bounty_on}',
		array(
			'db_ships' => $dbtables['ships'],
			'db_bounty' => $dbtables['bounty'],
			'bounty_on' => $bounty_on
		)
	);
	
	$j = 0;
	
	if ($smcFunc['db_num_rows']($result) > 0)
		while ($bounty_details[$j] = $smcFunc['db_fetch_assoc']($result))
			$j++;

	$smcFunc['db_free_result']($result);
	
	$num_details = $j;
  
	if ($num_details < 1)
		echo '<p>', $txt['bnt_by_nobounties'], '</p>';
	else
	{
		echo '<p>', $txt['bnt_by_bountyon'], ' <strong>',  $bounty_details[0]['character_name'], '</b></p>';
		echo '	
								<script type="text/javascript">
									$(document).ready(function(){
										// Table sorting function
										$(".bnt_table").tablesorter({
											widgets: ["zebra"]
										});
									});
								</script>
								<table class="bnt_table" width="50%">
									<thead>
										<tr>
											<th>', $txt['bnt_amount'], '</th>
											<th>', $txt['bnt_by_placedby'], '</th>
											<th>', $txt['bnt_by_action'], '</th>
										</tr>
									</thead>';
	 
		for ($j = 0; $j < $num_details; $j++)
		{
			$result = $smcFunc['db_query']('', '
				SELECT character_name
				FROM {raw:db_ships}
				WHERE ship_id = {int:placed_by}',
				array(
					'db_ships' => $dbtables['ships'],
					'placed_by' => $bounty_details[$j]['placed_by']
				)
			);
			$details = $smcFunc['db_fetch_assoc']($result);
			$smcFunc['db_free_result']($result);
			
			echo '	
									<tr>
										<td>',  NUMBER($bounty_details[$j]['amount']), '</td>';
						
			if ($bounty_details[$j]['placed_by'] == 0)
				echo '					<td>', $txt['bnt_by_thefeds'], '</td>';
			else
				echo '					<td>', $details['character_name'], '</td>';

			if ($bounty_details[$j]['placed_by'] == $playerinfo['ship_id'])
				echo '					<td><a href=', $scripturl, '?action=blacknova;sa=bounty;bid=', $bounty_details[$j]['bounty_id'], ';response=cancel>', $txt['bnt_by_cancel'], '</a></td>';
			else
				echo '					<td></td>';
			
			echo '						</tr>';
		}
		echo '						</table>';
	}
}
elseif ($response == "cancel")
{
	bigtitle($title);
	
	if ($playerinfo['turns'] < 1 )
	{
		echo '<p>', $txt['bnt_by_noturn'], '</p>';
		TextMainMenu();
		return;
	}
  
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_bounty}
		WHERE bounty_id = {int:bounty_id}',
		array(
			'db_bounty' => $dbtables['bounty'],
			'bounty_id' => $bid
		)
	);
  
	if ($smcFunc['db_num_rows']($result) == 0)
	{
		echo '<p>', $txt['bnt_by_nobounty'], '</p>';
		TextMainMenu();
		return;
	}
	
	$bty = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	if ($bty['placed_by'] <> $playerinfo['ship_id'])
	{
		echo '<p>', $txt['bnt_by_notyours'], '</p>';
		TextMainMenu();
		return;
	}
	
	$delete = $smcFunc['db_query']('', '
		DELETE FROM {raw:db_bounty}
		WHERE bounty_id = {int:bounty_id}',
		array(
			'db_bounty' => $dbtables['bounty'],
			'bounty_id' => $bid
		)
	);
	
	$refund = $bty['amount'];
  
	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_ships}
		SET last_login = NOW(),
			turns = turns - 1,
			turns_used = turns_used + 1,
			credits = credits + {float:refund}
		WHERE ship_id = {int:ship_id}',
		array(
			'db_ships' => $dbtables['ships'],
			'ship_id' => $playerinfo['ship_id'],
			'refund' => floatval($refund)
		)
	);
	
	echo '<p>', $txt['bnt_by_canceled'], '</p>';
	TextMainMenu();
	return;
}
elseif ($response == "place")
{
	bigtitle($title);
	$bounty_on = stripnum($bounty_on);
	
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_ships}
		WHERE ship_id = {int:bounty_on}',
		array(
			'db_ships' => $dbtables['ships'],
			'bounty_on' => $bounty_on
		)
	);
  
	if ($smcFunc['db_num_rows']($result) == 0)
	{
		echo '<p>', $txt['bnt_by_notexists'], '</p>';
		TextMainMenu();
		return;
	}
  
	$bty = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
  
	if ($bty['ship_destroyed'] == "Y")
	{
		echo '<p>', $txt['bnt_by_destroyed'], '</p>';
		TextMainMenu();
		return;
	}
  
	if ($playerinfo['turns'] < 1)
	{
		echo '<p>', $txt['bnt_by_noturn'], '</p>';
		TextMainMenu();
		return;
	}
	
	$amount = round(floatval(stripnum($amount)));
	
	if ($amount < 0)
	{
		echo '<p>', $txt['bnt_by_zeroamount'], '</p>';
		TextMainMenu();
		return;
	}
  
	if ($bounty_on == $playerinfo['ship_id'])
	{
		echo '<p>', $txt['bnt_by_yourself'], '</p>';
		TextMainMenu();
		return;
	}
  
	if ($amount > $playerinfo['credits'])
	{
		echo '<p>', $txt['bnt_by_notenough'], '</p>';
		TextMainMenu();
		return;
	}
  
	if ($bounty_maxvalue != 0)
	{
		$percent = $bounty_maxvalue * 100;
		$score = gen_score($playerinfo['ship_id']);
		$maxtrans = $score * $score * $bounty_maxvalue;
		$previous_bounty = 0;
		
		$result = $smcFunc['db_query']('', '
			SELECT SUM(amount) AS totalbounty
			FROM {raw:db_bounty}
			WHERE bounty_on = {int:bounty_on}
			AND placed_by = {int:ship_id}',
			array(
				'db_bounty' => $dbtables['bounty'],
				'bounty_on' => $bounty_on,
				'ship_id' => $playerinfo['ship_id']
			)
		);
		
		if ($smcFunc['db_num_rows']($result) > 0)
		{
			$prev = $smcFunc['db_fetch_assoc']($result);
			$previous_bounty = $prev['totalbounty'];
		}
		$smcFunc['db_free_result']($result);
	 
		if ($amount + $previous_bounty > $maxtrans)
		{   
			echo '<p>', $txt['bnt_by_toomuch'], '</p>';
			TextMainMenu();
			return;
		}

	}
	
	$smcFunc['db_insert']('insert',
		$dbtables['bounty'],
		array(
			'bounty_on' => 'int',
			'placed_by' => 'int',
			'amount' => 'float'
		),
		array(
			$bounty_on,
			$playerinfo['ship_id'],
			$amount
		),
		''
	);
	
	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_ships}
		SET last_login = NOW(),
			turns = turns - 1,
			turns_used = turns_used + 1,
			credits = credits - {float:amount}
		WHERE ship_id = {int:ship_id}',
		array(
			'db_ships' => $dbtables['ships'],
			'amount' => $amount,
			'ship_id' => $playerinfo['ship_id']
		)
	);
	
	echo '<p>', $txt['bnt_by_placed'], '</p>';
	TextMainMenu();
	return;
}
else
{
	bigtitle($title);
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_ships}
		WHERE ship_destroyed = "N"
		AND ship_id <> {int:ship_id}
		ORDER BY character_name ASC',
		array(
			'db_ships' => $dbtables['ships'],
			'ship_id' => $playerinfo['ship_id']
		)
	);
	echo '
								<script type="text/javascript">
									$(document).ready(function(){
										// Table sorting function
										$(".bnt_table").tablesorter({
											widgets: ["zebra"]
										});
									});
								</script>
								<form action=', $scripturl, '?action=blacknova;sa=bounty method=post>
									<table>
										<tr>
											<td>', $txt['bnt_by_bountyon'], '</td>
											<td>
												<select name="bounty_on">';
  
	while ($row = $smcFunc['db_fetch_assoc']($result))
	{
		if (isset($bounty_on) && $bounty_on == $row['ship_id'])
			$selected = "selected";
		else
			$selected = "";
			
		$charname = $row['character_name'];
		$ship_id = $row['ship_id'];
		echo '										<option value=', $ship_id, ' ', $selected, '>', $charname, '</option>';
	}
	$smcFunc['db_free_result']($result);
	
	echo '				
												</select>
											</td>
										</tr>
										<tr>
											<td>', $txt['bnt_by_amount'], ':</td>
											<td><input type="text" name="amount" size="20" maxlength="20" /></td>
										</tr>
										<tr>
											<td></td>
											<td><input type="submit" value="', $txt['bnt_by_place'], '" /><input type="reset" value="clear" /></td>
										</tr>
									</table>
									<input type="hidden" name="response" value="place">
								</form>';
	
	$result = $smcFunc['db_query']('', '
		SELECT bounty_on, SUM(amount) AS total_bounty
		FROM {raw:db_bounty}
		GROUP BY bounty_on',
		array(
			'db_bounty' => $dbtables['bounty'],
			'ship_id' => $playerinfo['ship_id']
		)
	);

	$i = 0;
	if ($smcFunc['db_num_rows']($result) > 0)
	{
		while ($row = $smcFunc['db_fetch_assoc']($result))
		{
			$bounties[$i] = $row;
			$i++;
		} 
	}
	$smcFunc['db_free_result']($result);

	$num_bounties = $i;
	
	if ($num_bounties < 1)
		echo $txt['bnt_by_nobounties'], '<br>';
	else
	{
		echo $txt['bnt_by_moredetails'], '<br><br>
								<table class="bnt_table">
									<thead>
										<tr>
											<th>', $txt['bnt_by_bountyon'], '</th>
											<th>', $txt['bnt_amount'], '</th>
										</tr>
									</thead>';

		for ($i = 0; $i < $num_bounties; $i++)
		{
			$result = $smcFunc['db_query']('', '
				SELECT character_name
				FROM {raw:db_ships}
				WHERE ship_id = {int:bounty_on}',
				array(
					'db_ships' => $dbtables['ships'],
					'bounty_on' =>  $bounties[$i]['bounty_on']
				)
			);
			$details = $smcFunc['db_fetch_assoc']($result);
			$smcFunc['db_free_result']($result);
			
			echo '	
									<tr>
										<td><a href="', $scripturl, '?action=blacknova;sa=bounty;bounty_on=', $bounties[$i]['bounty_on'], ';response=display">', $details['character_name'], '</a></td>
										<td>', Number($bounties[$i]['total_bounty']), '</td>
									</tr>';
		}
		echo '					</table>';
	}
	echo '						<br><br>';
}

?>
